//基础URL，请求的前缀
// #ifdef MP-WEIXIN
const BASE_URL = 'https://ai.xuexiluxian.cn'
// #endif

// #ifdef APP
const BASE_URL = 'https://ai.xuexiluxian.cn'
// #endif

// #ifdef H5
const BASE_URL = ''
// #endif

//请求超时
const TIMEOUT = 6000;



//请求拦截器 : 传递token
const requestIntercepted = ( config )=>{
	return config;
}
//取消重复的请求
const removePendingRequest = ( config )=>{
	//console.log('取消重复的请求' , config  )
}
//响应拦截器
const responseIntercepted = (response)=>{
	return response;
}
export default {
	/**
	 * @param {Object} options - 请求选项
	 * @return {  Promise } - 返回一个Promise对象
	 */
	request( options ){
		return new Promise((resolve,reject)=>{
			const finalOptions = {
				...options,
				url: BASE_URL ? BASE_URL + options.url : options.url, //这个可以不写入，在options里即可
				timeout:TIMEOUT,
				header:{
					'Content-type':'application/json',
					...options.header
				}
			}
			//请求拦截器
			const interceptedOptions = requestIntercepted( finalOptions );
			//取消重复的请求
			removePendingRequest( interceptedOptions );
			//发送请求
			const requestTask = uni.request({
				...interceptedOptions,
				success:( response )=>{
					//使用响应式拦截器
					let res = responseIntercepted( response );
					if( res.statusCode >= 200 &&  res.statusCode < 300 ){
						resolve( res.data  )
					}else{
						reject(  res  )
					}
				}
			})
		})
	},
	post(url, data = {} , options = {} ){
		return this.request({
			url,
			data,
			method:'post',
			...options
		})
	},
	get(url, data = {} , options = {} ){
		return this.request({
			url,
			data,
			method:'get',
			...options
		})
	}
}